package com.app.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.app.biz.domain.BizCityIndustrialEconomics;
import com.app.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.app.biz.mapper.BizIndustryAddedMapper;
import com.app.biz.domain.BizIndustryAdded;
import com.app.biz.service.IBizIndustryAddedService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 分行业工业增加值同比Service业务层处理
 * 
 * @author Zhou_S
 * @date 2024-01-02
 */
@Service
public class BizIndustryAddedServiceImpl implements IBizIndustryAddedService 
{
    @Autowired
    private BizIndustryAddedMapper bizIndustryAddedMapper;

    /**
     * 查询分行业工业增加值同比
     * 
     * @param baseId 分行业工业增加值同比主键
     * @return 分行业工业增加值同比
     */
    @Override
    public BizIndustryAdded selectBizIndustryAddedByBaseId(String baseId)
    {
        return bizIndustryAddedMapper.selectBizIndustryAddedByBaseId(baseId);
    }

    /**
     * 查询分行业工业增加值同比列表
     * 
     * @param bizIndustryAdded 分行业工业增加值同比
     * @return 分行业工业增加值同比
     */
    @Override
    public List<BizIndustryAdded> selectBizIndustryAddedList(BizIndustryAdded bizIndustryAdded)
    {
        return bizIndustryAddedMapper.selectBizIndustryAddedList(bizIndustryAdded);
    }

    /**
     * 新增分行业工业增加值同比
     * 
     * @param bizIndustryAdded 分行业工业增加值同比
     * @return 结果
     */
    @Override
    public int insertBizIndustryAdded(BizIndustryAdded bizIndustryAdded)
    {
        bizIndustryAdded.setBaseId(IdUtil.simpleUUID());
        bizIndustryAdded.setBaseCreateTime(new Date());
        DateTime datesTimes= DateUtil.parse(bizIndustryAdded.getBaseMonth(),"yyyy年MM月");
        bizIndustryAdded.setDates(DateUtil.format(datesTimes,"yyyy-MM"));
        return bizIndustryAddedMapper.insertBizIndustryAdded(bizIndustryAdded);
    }

    /**
     * 修改分行业工业增加值同比
     * 
     * @param bizIndustryAdded 分行业工业增加值同比
     * @return 结果
     */
    @Override
    public int updateBizIndustryAdded(BizIndustryAdded bizIndustryAdded)
    {
        DateTime datesTimes= DateUtil.parse(bizIndustryAdded.getBaseMonth(),"yyyy年MM月");
        bizIndustryAdded.setDates(DateUtil.format(datesTimes,"yyyy-MM"));
        return bizIndustryAddedMapper.updateBizIndustryAdded(bizIndustryAdded);
    }

    /**
     * 批量删除分行业工业增加值同比
     * 
     * @param baseIds 需要删除的分行业工业增加值同比主键
     * @return 结果
     */
    @Override
    public int deleteBizIndustryAddedByBaseIds(String[] baseIds)
    {
        return bizIndustryAddedMapper.deleteBizIndustryAddedByBaseIds(baseIds);
    }

    /**
     * 删除分行业工业增加值同比信息
     * 
     * @param baseId 分行业工业增加值同比主键
     * @return 结果
     */
    @Override
    public int deleteBizIndustryAddedByBaseId(String baseId)
    {
        return bizIndustryAddedMapper.deleteBizIndustryAddedByBaseId(baseId);
    }

<<<<<<< HEAD
    /**
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean transferDataInOneClick() {
        List<BizIndustryAdded> bizIndustryAddeds = bizIndustryAddedMapper.selectBizIndustryAddedAllData();
        int count = 0;
        List<BizIndustryAdded> list = new ArrayList<>();
        for(BizIndustryAdded item : bizIndustryAddeds){
            list.add(item);
            count++;
            if(count >= 100){
                bizIndustryAddedMapper.batchInsertBizIndustryAdded(list);
                list.clear();
            }
        }
        if(!list.isEmpty()){
            bizIndustryAddedMapper.batchInsertBizIndustryAdded(list);
            list.clear();
        }

        return true;
    }

    /**
     *
     */
    @Override
    public void clearAllTableData() {
        bizIndustryAddedMapper.clearAllTableData();
    }

    /**
     * <p>
     * 查询数据库中数据的日期(year_month字段)并进行去重复操作<br/>
     * 主要用于的是APP端左上角的日期过滤
     * </p>
     *
     * @return 日期的数组
     */
    @Override
    public List<String> getUniqueDatesFromDatabase() {
        List<String> uniqueDatesFromDatabase = bizIndustryAddedMapper.getUniqueDatesFromDatabase();
        if(!uniqueDatesFromDatabase.isEmpty()){
            //Mysql中去重关键字和排序不能对同一字段同时使用，这里通过代码来去重
            List<String> collect = uniqueDatesFromDatabase.stream().distinct().collect(Collectors.toList());
            collect = collect.stream().map(item -> {
                String[] split = item.split("-");
                return split[0] + "年" + split[1] + "月";
            }).collect(Collectors.toList());
            return collect;
        }
        return uniqueDatesFromDatabase;
    }
=======
>>>>>>> 7c95c84 (添加正式库模块)
}
